package ru.zombator.taskman.client.query;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import ru.zombator.taskman.client.query.corba.CorbaClient;
import ru.zombator.taskman.client.query.rmi.RmiClient;
import ru.zombator.taskman.client.query.tcp.TcpClient;
import ru.zombator.taskman.client.query.udp.UdpClient;
import ru.zombator.taskman.core.query.Client;
import ru.zombator.taskman.core.query.IClient;
import ru.zombator.taskman.core.query.Protocol;

/**
 * Фабрика клиентских объектов, отсылающих запросы серверу и
 * возвращающих ответы.
 * 
 * @author nvamelichev
 *
 */
public class ClientFactory {
	public static IClient createClient(Protocol proto) throws ClientInitException {
		String serverIp = System.getProperty("ru.zombator.taskman.client.serverIp");
		
		switch (proto) {
		case RMI:
			try {
				return new Client(new RmiClient(serverIp));
			} catch (MalformedURLException e) {
				throw new ClientInitException(e);
			} catch (RemoteException e) {
				throw new ClientInitException(e);
			} catch (NotBoundException e) {
				throw new ClientInitException(e);
			}
		case TCP:
			try {
				return new Client(new TcpClient(serverIp));
			} catch (IOException e) {
				throw new ClientInitException(e);
			}
		case UDP:
			try {
				return new Client(new UdpClient(serverIp));
			} catch (IOException e) {
				throw new ClientInitException(e);
			}
		case CORBA:
			try {
				return new Client(new CorbaClient(serverIp));
			} catch (NotFound e) {
				throw new ClientInitException(e);
			} catch (CannotProceed e) {
				throw new ClientInitException(e);
			} catch (InvalidName e) {
				throw new ClientInitException(e);
			}
		}
		return null;
	}
}
